package com.cysyz.mylib.dao.sqloperaterrevolver.impl;

import com.cysyz.mylib.utils.MyStringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by cy on 2014/7/29.
 */
public class InCsvValueOperatorRevolver extends InOperatorRevolver {
    private Pattern pattern = Pattern.compile("([\\d]+,){0,}[\\d]+");
    @Override
    public void addCond(StringBuilder sqlBuilder, List<Object> params, String column, String operator, Object value) {
        if (value != null) {
            String valCsv = (String) value;
            if (!valCsv.trim().isEmpty()) {
                List<Integer> valList = MyStringUtils.csvToList(valCsv, Integer.class);
                super.addCond(sqlBuilder, params, column, operator, valList);
            }
        }
    }



    @Override
    public boolean support(StringBuilder sqlBuilder, List<Object> params, String column, String operator, Object value) {
        boolean retVal = false;
        if (value != null) {
            retVal = super.support(sqlBuilder, params, column, operator, value);
            if(retVal && value instanceof String && pattern.matcher((CharSequence) value).matches()) {
                retVal = true;
            } else {
                retVal = false;
            }
        }
        return retVal;
    }


    @Override
    protected List<String> getSupportOperatorList() {
        List<String> supportList = new ArrayList<String>();
        supportList.add("in");
        return supportList;
    }

    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("([\\d\\w]+,){0,}[\\d\\w]+");
        System.out.println(pattern.matcher("de11,24dg,").matches());
    }
}
